package com.wlr.study.leetcode;

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author lerong.wang
 * @version 1.0.0
 * @description
 * @date 2024/12/20 17:41
 */
public class LeetCode102 {
    public List<List<Integer>> levelOrder(TreeNode root) {
        Deque<TreeNode> queue = new LinkedList<TreeNode>();
        if (root != null) queue.add(root);
        List<List<Integer>> res = new LinkedList<>();
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> list = new LinkedList<>();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.getFirst();
                queue.removeFirst();
                list.add(node.val);
                if (node.left != null) queue.add(node.left);
                if (node.right != null) queue.add(node.right);
            }
            res.add(list);
        }
        return res;
    }

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode();
        treeNode.val=1;
        treeNode.left=new TreeNode(4);
        treeNode.right = new TreeNode(2);
        treeNode.right.left = new TreeNode(3);
        System.out.println(new LeetCode102().levelOrder(treeNode));
    }
}